package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.CardCode;
import com.ruoyi.common.entity.Project;
import com.ruoyi.common.entity.dto.CardCodeDto;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.service.ICardCodeService;
import com.ruoyi.common.service.ProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 卡密Controller
 * 
 * @author ruoyi
 * @date 2025-08-17
 */
@RestController
@RequestMapping("/project/projectDetail")
public class ProjectController extends BaseController
{
    @Autowired
    private ProjectService projectService;

    /**
     * 查询项目列表（分页）
     * GET /api/projects - 获取资源集合
     */
   /* @GetMapping
    public Result<?> getProjects(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String tag) {

        Page<Project> page = new Page<>(pageNum, pageSize);
        Map<String, Object> query = Map.of(
                "title", title,
                "status", status,
                "tag", tag
        );

        IPage<Project> projectPage = projectService.listProjects(page, query);
        return Result.success(projectPage.getRecords(), projectPage.getTotal());
    }
*/
    /**
     * 查询单个项目详情
     * GET /api/projects/{id} - 获取单个资源
     */
    @GetMapping("/{id}")
    public R<Project> getProject(@PathVariable Long id) {
        Project project = projectService.getProjectById(id);

        if (project != null) {
            return  R.ok(project);
        } else {
            return R.fail(HttpStatus.NOT_FOUND, "项目不存在");
        }
    }

    /**
     * 新增项目
     * POST /api/projects - 创建资源
     */
    @PostMapping
    public R<?> createProject(@Validated @RequestBody Project project) {
        try {
            boolean success = projectService.addProject(project);
            if (success) {
                return  R.ok(project, "项目创建成功");
            } else {
                return R.fail("项目创建失败");
            }
        } catch (Exception e) {
            return R.fail("项目创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新项目（全量更新）
     * PUT /api/projects/{id} - 全量更新资源
     */
    @PutMapping("/{id}")
    public R<?> updateProject(
            @PathVariable Long id,
            @Validated @RequestBody Project project) {
        // 确保路径ID与请求体ID一致
        if (!id.equals(project.getId())) {
            return R.fail(HttpStatus.BAD_REQUEST, "路径ID与请求体ID不一致");
        }

        try {
            boolean success = projectService.updateProject(project);
            if (success) {
                return  R.ok(null, "项目更新成功");
            } else {
                return R.fail(HttpStatus.NOT_FOUND, "项目不存在或更新失败");
            }
        } catch (Exception e) {
            return R.fail("项目更新失败: " + e.getMessage());
        }
    }

    /**
     * 部分更新项目
     * PATCH /api/projects/{id} - 部分更新资源
     */
    @PatchMapping("/{id}")
    public R<?> partialUpdateProject(
            @PathVariable Long id,
            @RequestBody Project project) {
        // 仅更新提供的字段，确保ID一致
        project.setId(id);

        try {
            boolean success = projectService.updateProject(project);
            if (success) {
                return R.ok(null, "项目更新成功");
            } else {
                return R.fail(HttpStatus.NOT_FOUND, "项目不存在或更新失败");
            }
        } catch (Exception e) {
            return R.fail("项目更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除项目
     * DELETE /api/projects/{id} - 删除资源
     */
    @DeleteMapping("/{id}")
    public void deleteProject(@PathVariable Long id) {
        boolean success = projectService.deleteProject(id);
        if (!success) {
            throw new RuntimeException("项目删除失败");
        }
    }
}
